package org.springframework.xml.xpath.proxy.conversion.base;

import org.springframework.beans.propertyeditors.CustomBooleanEditor;

/**
 * Normally xpath uses the existance of an element or attribute to determine
 * its boolean state.
 * <p/>
 * This BooleanConverter uses a spring {@link CustomBooleanEditor}.
 *
 * @author Ray Krueger
 */
public class BooleanConverter extends PropertyEditorConverterAdapter<Boolean> {

    public BooleanConverter() {
        super(Boolean.class, new CustomBooleanEditor(true));
    }

    protected Boolean convertString(String input) {
        CustomBooleanEditor customBooleanEditor = new CustomBooleanEditor(true);
        customBooleanEditor.setAsText(input);
        Object value = customBooleanEditor.getValue();
        return value != null && (Boolean) value;
    }

    protected Class<Boolean> getSupportedType() {
        return Boolean.class;
    }
}
